/**
 * 文件名称:  ParamSetController.java
 * 文件序号:  序号
 * 内容描述:  业务参数设置controller类
 * 版权声明:  山东鸿蝶信息技术有限公司
 * 创建用户:  MissJin
 * 创建日期:  2017/1/18/17:29
 * 修改用户:  无
 * 修改日期:  无
 * 修改说明:  无
 * 文件版本:  V1.0
 * 文件路径:
 */
package com.hongdee.base.param.controller;


import com.hongdee.base.entity.ResultEntity;
import com.hongdee.base.param.service.IParamSetService;
import com.hongdee.common.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 名  称: ParamSetController
 * 类  型: class
 * 描  述: 业务参数设置controller类
 * 路  径: com.hongdee.
 * 作  者: MissJin
 * 日  期: 2017/1/18/17:30
 */
@RestController
@RequestMapping("/base/param/set")
public class ParamSetController {
    @Autowired
    private IParamSetService iParamSetService;

    /**
     * @param uid     登录用户id
     * @param token   登录用户 token
     * @param oId     部门
     * @param sysCode 模块
     * @param defType 参数类型
     * @return Map
     * @throws Exception
     * @desc 查询参数值
     * @author 徐钦勇
     */
    @RequestMapping("/info")
    @ResponseBody
    public ResultEntity info(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "oId", defaultValue = "") String oId,
                             @RequestParam(name = "sysCode", defaultValue = "") String sysCode,
                             @RequestParam(name = "defType", defaultValue = "") String defType) throws Exception {
        ResultEntity res = new ResultEntity();
        List<Map<String, Object>> map = iParamSetService.info(oId, defType, sysCode);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }

    /**
     * @param uid   登录用户id
     * @param token 登录用户 token
     * @return res
     * @throws Exception
     * @desc 参数设置实体保存
     * @author 徐钦勇
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResultEntity save(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody List<Map<String, Object>> nodes) throws Exception {

        ResultEntity res = new ResultEntity();
        String companyId = TokenUtil.getCompanyId(uid);
        iParamSetService.save(nodes, companyId);
        res.setSuccess(true);
        res.setMsg("保存成功");
        return res;
    }

    /**
     * @param uid   登录用户id
     * @param token 登录用户 token
     * @param start 当前页码
     * @param limit 页面大小
     * @return Map
     * @throws Exception
     * @desc page分页查询
     * @author 徐钦勇
     */
    @RequestMapping("/page")
    @ResponseBody
    public Map page(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "0") String start,
                    @RequestParam(name = "limit", defaultValue = "50") String limit,
                    @RequestParam(name = "pCode", defaultValue = "") String pCode) throws Exception {
        Map map = iParamSetService.page(start, limit, pCode, TokenUtil.getCompanyId(uid));
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }

    /*
     * paramSet defTree
     */
    @RequestMapping("/defTree")
    @ResponseBody
    public ResultEntity defTree(@RequestParam(name = "uid", defaultValue = "") String uid,
                                @RequestParam(name = "token", defaultValue = "") String token) throws Exception {
        ResultEntity res = new ResultEntity();
        res.setDataList(this.iParamSetService.defTree());
        res.setSuccess(true);
        res.setMsg("获取参数树成功！");
        return res;
    }

    /*
    * paramSet defTree
    */
    @RequestMapping("/setTree")
    @ResponseBody
    public ResultEntity setTree(@RequestParam(name = "uid", defaultValue = "") String uid,
                                @RequestParam(name = "token", defaultValue = "") String token,
                                @RequestParam(name = "pCode", defaultValue = "") String pCode) throws Exception {
        ResultEntity res = new ResultEntity();
        res.setDataList(this.iParamSetService.setTree(TokenUtil.getCompanyId(uid),pCode));
        res.setSuccess(true);
        res.setMsg("获取参数树成功！");
        return res;
    }

    @RequestMapping("/checkedDef")
    @ResponseBody
    public ResultEntity checkedDef(@RequestParam(name = "uid", defaultValue = "") String uid,
                                   @RequestParam(name = "token", defaultValue = "") String token) throws Exception {
        ResultEntity res = new ResultEntity();
        res.setDataList(this.iParamSetService.checkedDef(TokenUtil.getCompanyId(uid)));
        res.setSuccess(true);
        res.setMsg("获取参数树成功！");
        return res;
    }

    @RequestMapping("/changValue")
    @ResponseBody
    public ResultEntity changValue(@RequestParam(name = "uid", defaultValue = "") String uid,
                                   @RequestParam(name = "token", defaultValue = "") String token,
                                   @RequestParam(name = "id", defaultValue = "") String id,
                                   @RequestParam(name = "value", defaultValue = "") String value) throws Exception {
        if (StringUtils.isEmpty(id)) {
            throw new Exception("请选择记录");
        }
        ResultEntity res = new ResultEntity();
        this.iParamSetService.changValue(id, value);
        res.setSuccess(true);
        res.setMsg("保存成功");
        return res;
    }




}
